BINARY = envelope
CONFIGS = -c config/envelope.conf -t 300 -i t
WEBROOTS = -r ./web_root/ -z ./role -y ./app
INSTALL_DEPS = @prefix@/sbin/ @prefix@/etc/envelope/ \
@prefix@/etc/envelope/envelope.conf @prefix@/etc/envelope/envelope-connections.conf \
@prefix@/etc/envelope/role/ \
@prefix@/etc/envelope/app/all/file_manager_new/ \
@prefix@/etc/envelope/app/developer_g/greyspots-1.1.1/ \
@prefix@/etc/envelope/web_root/ \
@prefix@/etc/envelope/app/all/file_manager/ \
@prefix@/etc/envelope/app/all/index.html
SOCK_DIR_PARAMETER = @sock_dir_parameter@

VERSION = \"$(shell cat ../VERSION)\"
CC = cc
CFLAGS = -O3 -g \
	-fno-strict-aliasing
LDFLAGS = @LDFLAGS@
CPPFLAGS = @CPPFLAGS@ \
	-I../db_framework_pq -I../ -I../common -I../dependencies/tre/include -I../dependencies/libev \
	-DVERSION=$(VERSION) -DPOSTAGE_PREFIX=\"@prefix@\" -DPOSTAGE_INTERFACE_LIBPQ -DENVELOPE -DTRE_VERSION=\"0.8.0\"
V=0

common_SOURCES = $(wildcard ../common/*.c) $(filter-out ../util/util_getopt.c, $(filter-out ../util/util_strptime.c, $(filter-out ../util/util_exec.c, $(wildcard ../util/*.c))))
db_framework_pq_SOURCES = $(wildcard ../db_framework_pq/*.c)

envelope_SOURCES = $(sort $(common_SOURCES) $(db_framework_pq_SOURCES) $(wildcard *.c))
envelope_OBJECTS = $(envelope_SOURCES:.c=.o)

libtre_SOURCES = $(wildcard ../dependencies/tre/lib/*.c)
libtre_OBJECTS = $(libtre_SOURCES:.c=.o)

libev_SOURCES = ../dependencies/libev/ev.c
libev_OBJECTS = $(libev_SOURCES:.c=.o)

.PHONY: all all-format destroy check

# this needs to be at the top to be the default target
all: $(BINARY)
	@echo
	@echo "Compilation Successful"

#### DEPENDENCIES
$(libev_OBJECTS): %.o: %.c
ifeq ($(V), 1)
	$(CC) $(CFLAGS) $(CPPFLAGS) @libev_CPPFLAGS@ $< -c -o $@
else
	@echo " CC $@"
	@$(CC) $(CFLAGS) $(CPPFLAGS) @libev_CPPFLAGS@ $< -c -o $@
endif

$(libtre_OBJECTS): %.o: %.c
ifeq ($(V), 1)
	$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@
else
	@echo " CC $@"
	@$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@
endif

#### BUILDING
$(BINARY): $(BUILD_DEPS) $(libtre_OBJECTS) $(libev_OBJECTS) $(envelope_OBJECTS)
ifeq ($(V), 1)
	$(CC) $(libtre_OBJECTS) $(libev_OBJECTS) $(envelope_OBJECTS) $(LDFLAGS) $(CFLAGS) -o ./$(BINARY)
else
	@echo " CCLD $(BINARY)"
	@$(CC) $(libtre_OBJECTS) $(libev_OBJECTS) $(envelope_OBJECTS) $(LDFLAGS) $(CFLAGS) -o ./$(BINARY)
endif

all-format:
	cd ..; ./cleanup_format.sh; cd $(BINARY)
	$(MAKE) all

$(envelope_OBJECTS): %.o: %.c
ifeq ($(V), 1)
	$(CC) $(CFLAGS) $(CPPFLAGS) -Wall -Wextra -Wconversion $< -c -o $@
else
	@echo " CC $@"
	@$(CC) $(CFLAGS) $(CPPFLAGS) -Wall -Wextra -Wconversion $< -c -o $@
endif

#### CLEAN
clean:
	rm -f $(envelope_OBJECTS) $(libev_OBJECTS) $(libtre_OBJECTS) ../check/*.o $(BINARY)
	@echo
	@echo "Clean Successful"

#### INSTALL
@prefix@/sbin/:
	mkdir -p @prefix@/sbin/

@prefix@/etc/envelope/:
	mkdir -p @prefix@/etc/envelope/

@prefix@/etc/envelope/app/all/file_manager_new/:
	mkdir -p @prefix@/etc/envelope/app/all/file_manager_new/

@prefix@/etc/envelope/app/all/file_manager/:
	mkdir -p @prefix@/etc/envelope/app/all/file_manager/
	cp -r ./app/all/file_manager/* @prefix@/etc/envelope/app/all/file_manager/

@prefix@/etc/envelope/app/developer_g/greyspots-1.1.1/:
	mkdir -p @prefix@/etc/envelope/app/developer_g/greyspots-1.1.1/
	cp -r ./app/developer_g/greyspots-1.1.1/* @prefix@/etc/envelope/app/developer_g/greyspots-1.1.1/

@prefix@/etc/envelope/web_root/:
	mkdir -p @prefix@/etc/envelope/web_root
	cp -r ./web_root/* @prefix@/etc/envelope/web_root/

@prefix@/etc/envelope/app/all/index.html:
	install ./app/all/index.in.html @prefix@/etc/envelope/app/all/index.html

@prefix@/etc/envelope/role/:
	mkdir -p @prefix@/etc/envelope/role/all/
	cp -r ./role/all/*.txt @prefix@/etc/envelope/role/all/

@prefix@/etc/$(BINARY)/$(BINARY).conf: @prefix@/etc/$(BINARY)/ config/$(BINARY).conf
	test -f @prefix@/etc/$(BINARY)/$(BINARY).conf || install config/$(BINARY).conf @prefix@/etc/$(BINARY)/$(BINARY).conf;

@prefix@/etc/$(BINARY)/$(BINARY)-connections.conf: @prefix@/etc/$(BINARY)/ config/$(BINARY)-connections.conf
	test -f @prefix@/etc/$(BINARY)/$(BINARY)-connections.conf || install config/$(BINARY)-connections.conf @prefix@/etc/$(BINARY)/$(BINARY)-connections.conf;

install: $(INSTALL_DEPS)
	mkdir -p @prefix@/etc/$(BINARY)/ @prefix@/sbin @prefix@/@MANDIR@/man1
	install config/$(BINARY).conf @prefix@/etc/$(BINARY)/$(BINARY).conf.example
	install config/$(BINARY)-connections.conf @prefix@/etc/$(BINARY)/$(BINARY)-connections.conf.example
	install $(BINARY) @prefix@/sbin/
	install man/man1/envelope.1 @prefix@/@MANDIR@/man1/envelope.1
	install ./app/all/index.in.html @prefix@/etc/envelope/app/all/index.in.html
	cp -rf ./app/all/file_manager/* @prefix@/etc/envelope/app/all/file_manager_new/
	cp -r ./app/all/*.txt @prefix@/etc/envelope/app/all/
	@echo
	@echo "Installation Successful"

uninstall:
	rm @prefix@/sbin/$(BINARY)
	rm -f @prefix@/etc/$(BINARY)/*.example
	@echo
	@echo "Uninstall Successful"

destroy:
	@printf "Are you sure? This will delete all of your application HTML [Y/n]: "
	@{ \
		read; \
		if test "$$REPLY" = "Y" -o "$$REPLY" = "y"; then \
			rm @prefix@/sbin/$(BINARY); \
			rm -rf @prefix@/etc/$(BINARY); \
			echo; \
			echo "Destroy Successful"; \
		fi; \
	}

#### TESTING
test-common:
	-kill `cat postgres.pid`
	-rm postgres.pid
	-rm -rf data_test
	initdb -D data_test -E UTF8 -U postgres
	echo $(SOCK_DIR_PARAMETER)"='/tmp'" >> data_test/postgresql.conf
	{ postgres -D data_test -p 5431 2> output.txt > output.txt & }; \
	echo $$! > postgres.pid; \
	sleep 3
	psql -U postgres -h 127.0.0.1 -p 5431 postgres -f test.sql
	cat app/developer_g/greyspots-1.1.1/documentation/index.html | sed 's/normal_g/envelope_g/' | sed '/    var strPostgreSQL = ml(function () {\/\*/,/\*\//!d' | tail -n +2 | head -n -1 | psql -U postgres -h 127.0.0.1 -p 5431 postgres -1
	kill `cat postgres.pid`; rm postgres.pid; sleep 3
	cp data_test/pg_hba.conf data_test/pg_hba.bak
	sed -e '/trust/s/32            trust/32            md5/g' < data_test/pg_hba.bak > data_test/pg_hba.conf
	echo "host    all             all             192.168.0.0/16            md5" >> data_test/pg_hba.conf
	{ postgres -D data_test -i -p 5431 2> output.txt > output.txt & }; \
	echo $$! > postgres.pid; \
	sleep 3

test: test-common
	./$(BINARY) $(CONFIGS) -d config/envelope-connections-test.conf $(WEBROOTS) -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }

test-set-user: test-common
	./$(BINARY) $(CONFIGS) -d config/envelope-connections-set-user-test.conf $(WEBROOTS) -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }

test-ssl: test-common
	./$(BINARY) $(CONFIGS) -d config/envelope-connections-test.conf $(WEBROOTS) -j ../ssl/self.crt -k ../ssl/self.key -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }

gdb: test-common
	echo "run" > temp_gdb_commands
	gdb -x temp_gdb_commands --args ./$(BINARY) $(CONFIGS) -d config/envelope-connections-test.conf $(WEBROOTS) -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }
	rm temp_gdb_commands

gdb-set-user: test-common
	echo "run" > temp_gdb_commands
	gdb -x temp_gdb_commands --args ./$(BINARY) $(CONFIGS) -d config/envelope-connections-set-user-test.conf $(WEBROOTS) -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }
	rm temp_gdb_commands

gdb-ssl: test-common
	echo "run" > temp_gdb_commands
	gdb -x temp_gdb_commands --args ./$(BINARY) $(CONFIGS) -d config/envelope-connections-test.conf $(WEBROOTS) -j ../ssl/self.crt -k ../ssl/self.key -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }
	rm temp_gdb_commands

lldb: test-common
	echo "run" > temp_lldb_commands
	lldb -s temp_lldb_commands -- ./$(BINARY) $(CONFIGS) -d config/envelope-connections-test.conf $(WEBROOTS) -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }
	rm temp_lldb_commands

lldb-set-user: test-common
	echo "run" > temp_lldb_commands
	lldb -s temp_lldb_commands -- ./$(BINARY) $(CONFIGS) -d config/envelope-connections-set-user-test.conf $(WEBROOTS) -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }
	rm temp_lldb_commands

lldb-ssl: test-common
	echo "run" > temp_lldb_commands
	lldb -s temp_lldb_commands -- ./$(BINARY) $(CONFIGS) -d config/envelope-connections-test.conf $(WEBROOTS) -j ../ssl/self.crt -k ../ssl/self.key -l info -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }
	rm temp_lldb_commands

valgrind-set-user: test-common
	valgrind --leak-check=full --track-fds=yes --track-origins=yes --show-reachable=yes --suppressions=../valgrind/mac_suppressions.txt --num-callers=100 ./$(BINARY) $(CONFIGS) -d config/envelope-connections-set-user-test.conf $(WEBROOTS) -l error -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }

valgrind: test-common
	valgrind --leak-check=full --track-fds=yes --track-origins=yes --show-reachable=yes --suppressions=../valgrind/mac_suppressions.txt --num-callers=100 ./$(BINARY) $(CONFIGS) -d config/envelope-connections-test.conf $(WEBROOTS) -l error -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }

valgrind-ssl: test-common
	valgrind --leak-check=full --track-fds=yes --track-origins=yes --show-reachable=yes --suppressions=../valgrind/mac_suppressions.txt --num-callers=100 ./$(BINARY) $(CONFIGS) -d config/envelope-connections-test.conf $(WEBROOTS) -j ../ssl/self.crt -k ../ssl/self.key -l error -u public_user -w youwontguessthispasswordwillyou1lk2j43k1lj3h4k3j4h56k324j5bnoiusdfyg08 || \
	{ kill `cat postgres.pid`; rm postgres.pid; rm -rf data_test; exit 1; }
